import type { Module } from 'vuex'
import lkfRequest from '@/services'
import { IRootState } from '../types'
import { IMusicList } from './types'
/* 首页store */
const index: Module<IMusicList, IRootState> = {
  namespaced: true,
  state() {
    return {
      bannerList: [],
      topList: [],
      personalizedList: [],
      commmentList: []
    }
  },
  getters: {},
  mutations: {
    getBannerListGetter(state, list) {
      for (const item of list) {
        item.imageUrl += '?param=810x300'
      }
      state.bannerList = list
    },
    getTopListGetter(state, list) {
      for (const item of list.result) {
        item.picUrl += '?param=200x200'
      }
      state.topList = list.result
    },
    getPersonalizedListGetter(state, list) {
      for (const item of list.result) {
        item.picUrl += '?param=200x200'
      }
      state.personalizedList = list.result
    },
    getCommentListGetter(state, list) {
      for (const item of list.hotComments) {
        item.avatarUrl += '?param=200x200'
      }
      state.commmentList = list.hotComments
    }
  },
  actions: {
    /* 请求数据 */
    async getBannerListAction({ commit }) {
      const list = await lkfRequest.get({ url: '/banner' })
      commit('getBannerListGetter', list.banners)
    },
    async getTopListAction({ commit }) {
      const list = await lkfRequest.get({ url: '/personalized/newsong' })
      commit('getTopListGetter', list)
    },
    async getPersonalizedListAction({ commit }) {
      const list = await lkfRequest.get({ url: '/personalized?limit=10' })
      commit('getPersonalizedListGetter', list)
    },
    async getCommentListAction({ commit }, payload) {
      const list = await lkfRequest.get({
        url: `/comment/hot?id=${payload.id}&type=0`
      })

      commit('getCommentListGetter', list)
    }
  }
}
export default index
